package com.chinawayltd.ews.sample.service.impl;

import com.chinawayltd.ews.sample.mapper.TVehicleStatisticsMinuteMapper;
import com.chinawayltd.ews.sample.mapper.TaskProducerMapper;
import com.chinawayltd.ews.sample.model.TVehicleStatisticsMinute;
import com.chinawayltd.ews.sample.model.openApi.VehicleStatusDTO;
import com.chinawayltd.ews.sample.service.SynchroVehicleMinuteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @program: greeProject.timingTaskCenter
 * @description:
 * @author: xiazj
 * @create: 2018-05-25 13:36
 **/
@Service
public class SynchroVehicleMinuteServiceImpl implements SynchroVehicleMinuteService {

    @Autowired
    private TaskProducerMapper taskProducerMapper;
    @Autowired
    private TVehicleStatisticsMinuteMapper tVehicleStatisticsMinuteMapper;
    private SimpleDateFormat sdf_date_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    @Transactional
    public void generateVehicleMinuteData() throws ParseException {
        List<VehicleStatusDTO> list = taskProducerMapper.getVehicleStatusList();
        LocalDate now = LocalDate.now();
        String beginDate = now.toString();
        beginDate = beginDate+" 00:00:00";
        Date date = sdf_date_time.parse(beginDate);
        if(list != null && !list.isEmpty()){
            TVehicleStatisticsMinute vsm = null;
            for (VehicleStatusDTO dto : list) {
                vsm = new TVehicleStatisticsMinute();
                vsm.setVehicleId(dto.getVehicleId());
                vsm.setStatisticsTime(date);
                vsm.setCreateTime(new Date());
                vsm.setUpdateTime(new Date());
                if(dto.getExecutState() >0)
                    vsm.setIsExecute((byte) 1);
                else
                    vsm.setIsExecute((byte) 0);
                tVehicleStatisticsMinuteMapper.insert(vsm);
            }
        }
    }

}
